package com.example.trafficsystem.service;



import com.example.trafficsystem.entity.*;
import com.example.trafficsystem.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DataInsertService {

    @Autowired
    private ExamRe examRepository;
    @Autowired
    private ExamQuestionRe examQuestionRepository;
    @Autowired
    private ExamRecordRe examRecordRepository;
    @Autowired
    private ExamAnswerRe examAnswerRepository;

    public void insertTestData() {
        // 创建一个考试
        Exam exam = new Exam();
        exam.setName("Java基础考试");
        exam.setDescription("测试Java基础知识的考试");
        exam.setTotalScore(100);

        // 创建一个试题
        ExamQuestion question1 = new ExamQuestion();
        question1.setQuestionText("Java中的基本数据类型有哪些？");
        question1.setOption1("int, long, char, boolean");
        question1.setOption2("int, double, boolean");
        question1.setOption3("char, String, boolean");
        question1.setOption4("int, long, String, char");
        question1.setCorrectAnswer("int, long, char, boolean");
        question1.setScore(25);
        question1.setQuestionType("单选题");

        ExamQuestion question2 = new ExamQuestion();
        question2.setQuestionText("Java的继承是怎么实现的？");
        question2.setOption1("通过extends关键字");
        question2.setOption2("通过implements关键字");
        question2.setOption3("通过继承类的方法");
        question2.setOption4("Java不支持继承");
        question2.setCorrectAnswer("通过extends关键字");
        question2.setScore(25);
        question2.setQuestionType("单选题");

        // 将题目保存到数据库
        examQuestionRepository.save(question1);
        examQuestionRepository.save(question2);

        // 将题目添加到考试中
        exam.setQuestions(List.of(question1, question2));

        // 保存考试到数据库
        examRepository.save(exam);

        // 创建考试记录
        ExamRecord examRecord = new ExamRecord();
        examRecord.setExam(exam);
        examRecord.setStudent(new Student(1L));  // 假设学员ID为1

        examRecord.setStartTime(LocalDateTime.now());
        examRecord.setIsSubmitted(false);

        // 保存考试记录
        examRecordRepository.save(examRecord);

        // 创建答题记录
        ExamAnswer examAnswer1 = new ExamAnswer();
        examAnswer1.setExamRecord(examRecord);
        examAnswer1.setExamQuestion(question1);
        examAnswer1.setAnswer("int, long, char, boolean");
        examAnswer1.setIsCorrect(true);

        ExamAnswer examAnswer2 = new ExamAnswer();
        examAnswer2.setExamRecord(examRecord);
        examAnswer2.setExamQuestion(question2);
        examAnswer2.setAnswer("通过extends关键字");
        examAnswer2.setIsCorrect(true);

        // 保存答题记录
        examAnswerRepository.save(examAnswer1);
        examAnswerRepository.save(examAnswer2);

        // 更新考试记录的分数
        examRecord.setScore(50);  // 假设得分50
        examRecord.setSubmitTime(LocalDateTime.now());
        examRecord.setIsSubmitted(true);
        examRecordRepository.save(examRecord);
    }
}

